package com.xuecheng.learning.service;

import com.alibaba.druid.util.StringUtils;
import com.xuecheng.framework.domain.course.TeachPlanMediaPub;
import com.xuecheng.framework.domain.learning.XcLearningCourse;
import com.xuecheng.framework.domain.learning.response.GetMediaResult;
import com.xuecheng.framework.domain.learning.response.LearningCode;
import com.xuecheng.framework.domain.task.XcTask;
import com.xuecheng.framework.domain.task.XcTaskHis;
import com.xuecheng.framework.exception.ExceptionCast;
import com.xuecheng.framework.model.response.CommonCode;
import com.xuecheng.framework.model.response.ResponseResult;
import com.xuecheng.learning.client.CourseSearchClient;
import com.xuecheng.learning.dao.XcLearningCourseRepository;
import com.xuecheng.learning.dao.XcTaskHisRepository;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.Date;
import java.util.Optional;

/**
 * @author zhaoxiang
 * @version 1.0
 * @Date 2020/1/3 14:28
 */
@Service
public class CourseLearningService {

    @Autowired
    private CourseSearchClient courseSearchClient;
    @Autowired
    private XcTaskHisRepository xcTaskHisRepository;
    @Autowired
    private XcLearningCourseRepository xcLearningCourseRepository;


    /**
     * 获取课程学习地址（视频播放地址）
     *
     * @param courseId
     * @param teachPlanId
     * @return
     */
    public GetMediaResult getMedia(String courseId, String teachPlanId) {
        //校验学生的学习权限。。是否资费等
        //远程调用搜索服务查询课程计划所对应的课程媒资信息

        TeachPlanMediaPub teachPlanMediaPub = courseSearchClient.getmedia(teachPlanId);
        if (teachPlanMediaPub == null || StringUtils.isEmpty(teachPlanMediaPub.getMediaUrl())) {
            //获取视频播放地址出错
            ExceptionCast.cast(LearningCode.LEARNING_GETMEDIA_ERROR);
        }
        return new GetMediaResult(CommonCode.SUCCESS, teachPlanMediaPub.getMediaUrl());
    }

    /**
     * 添加选课
     *
     * @param userId
     * @param courseId
     * @param valid
     * @param startTime
     * @param endTime
     * @param xcTask
     * @return
     */
    @Transactional
    public ResponseResult addCourse(String userId, String courseId, String valid, Date
            startTime, Date endTime, XcTask xcTask){
        if (StringUtils.isEmpty(courseId)) {
            ExceptionCast.cast(LearningCode.LEARNING_GETMEDIA_ERROR);
        }
        if (StringUtils.isEmpty(userId)) {
            ExceptionCast.cast(LearningCode.CHOOSECOURSE_USERISNULL);
        }
        if(xcTask == null || StringUtils.isEmpty(xcTask.getId())){
            ExceptionCast.cast(LearningCode.CHOOSECOURSE_TASKISNULL);
        }
        //查询历史任务
        Optional<XcTaskHis> xcTaskHisOptional = xcTaskHisRepository.findById(xcTask.getId());
        if (xcTaskHisOptional.isPresent()){
            //如果有，则说明选课信息已经添加成功
            return new ResponseResult(CommonCode.SUCCESS);
        }
        //如果没有，则进行选课信息添加
        XcLearningCourse xcLearningCourse
                = xcLearningCourseRepository.findXcLearningCourseByUserIdAndCourseId(userId, courseId);
        //选课记录信息如果没有则添加
        if (xcLearningCourse == null){
            xcLearningCourse = new XcLearningCourse();
            xcLearningCourse.setUserId(userId);
            xcLearningCourse.setCourseId(courseId);
            xcLearningCourse.setValid(valid);
            xcLearningCourse.setStartTime(startTime);
            xcLearningCourse.setEndTime(endTime);
            xcLearningCourse.setStatus("501001");
            xcLearningCourseRepository.save(xcLearningCourse);
        }
        else {//有选课记录则更新选课日期
            xcLearningCourse.setValid(valid);
            xcLearningCourse.setStartTime(startTime);
            xcLearningCourse.setEndTime(endTime);
            xcLearningCourse.setStatus("501001");
            xcLearningCourseRepository.save(xcLearningCourse);
        }
        //向历史任务表插入记录
        Optional<XcTaskHis> optional = xcTaskHisRepository.findById(xcTask.getId());
        if(!optional.isPresent()){
            //添加历史任务
            XcTaskHis xcTaskHis = new XcTaskHis();
            BeanUtils.copyProperties(xcTask,xcTaskHis);
            xcTaskHisRepository.save(xcTaskHis);
        }
        return new ResponseResult(CommonCode.SUCCESS);
    }
}
